package com.elt.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.elt.cache.impl.ELTFileCache;
import com.elt.client.AsyncHttpRequest;
import com.elt.client.AsyncLoadImg;
import com.elt.dao.DBFactory;
import com.elt.dao.dao.IHttpDataDao;
import com.elt.dao.modle.HttpData;
import com.elt.http.async.callback.base.BaseHandler;
import com.elt.http.model.GetModel;
import com.elt.http.model.LoadImgModel;
import com.elt.util.BitmapMemeryCache;

public class ElongtianFramworkActivity extends Activity {
	/** Called when the activity is first created. */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// testGet();
		// testLoadImg();
		testCache();
		memeryCache();
		testDB();
	}

	private void testDB() {
		// TODO Auto-generated method stub
		IHttpDataDao db = DBFactory.createDataDao();
//		删除过期数据
		db.deleteDieData(6 * 1000);
//		根据id查询
		db.findById("husongzhen");
//		保存
		db.save(new HttpData(), "id");
	}

	// 缓存图片到内存中
	private void memeryCache() {
		// TODO Auto-generated method stub
		Bitmap reault = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		BitmapMemeryCache.put("key", reault);

	}

	private void testCache() {
		// TODO Auto-generated method stub

		// 文本緩存到本地
		ELTFileCache cache = new ELTFileCache();
		cache.put("husongzhen", "husongzhen");
		String reslult = cache.get("husongzhen");
		TextView text = (TextView) findViewById(R.id.text);
		text.setText(reslult);

		// 图片緩存到本地
		Bitmap reault = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		cache.put("bitmap", reault);

		Bitmap result = cache.getAsBitmap("bitmap");

		ImageView img = (ImageView) this.findViewById(R.id.img);
		img.setImageBitmap(result);

	}

	/**
	 * 1、獲取图片 2、更新
	 */

	private void testLoadImg() {

		// TODO Auto-generated method stub
		String url = "http://www.ibm.com/developerworks/cn/xml/x-androidstorage/fig01.gif";
		ImageView view = (ImageView) findViewById(R.id.img);

		AsyncLoadImg.loadImg(url, view, 100, 100,
				new BaseHandler<LoadImgModel>() {

					@Override
					public void succ(LoadImgModel result) {
						// TODO Auto-generated method stub
						result.getView().setImageBitmap(result.getBitmap());
					}

					@Override
					public void fail(Exception e) {
						// TODO Auto-generated method stub

					}
				});
	}

	// 获取http请求数据

	private void testGet() {
		// TODO Auto-generated method stub

		String url = "http://app.sigm.com.cn/index.php?com=com_appService&method=save&app_com=com_pro&content_type=img&children=all&data=1&app_method=lists&data=1&app_optionid=929&row=10&page=1";

		AsyncHttpRequest.getAsync(url, "929", false,
				new BaseHandler<GetModel>() {

					@Override
					public void succ(final GetModel result) {
						// TODO Auto-generated method stub
						final TextView text = (TextView) findViewById(R.id.text);
						text.setText(result.getResult().toString());
					}

					@Override
					public void fail(Exception e) {
						// TODO Auto-generated method stub
					}
				});

	}
}